package com.argticling.controller;

import java.util.ArrayList;

import com.argticling.model.Article;
import com.argticling.model.Reader;
import com.argticling.model.User;
import com.argticling.service.ArticleService;
import com.argticling.service.ReaderService;
import com.argticling.service.UserService;

import org.hibernate.mapping.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * Created by IntelliJ IDEA.
 * User: santor
 * Date: 08-nov-2010
 * Time: 19:49:05
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/reader")
public class ReaderController extends MultiActionController {

    @Autowired
    private ReaderService readerService;
    @Autowired 
    private ArticleService articleService;
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView addNewReader(
            @RequestParam("readerEmail") String readerEmail,
            @RequestParam("articleId") String articleId) 
    		{
    			
    	Article article = articleService.getArticle(articleId);
    	
		ModelAndView mav = new ModelAndView("reader/readArticle");
        mav.addObject("article", article);
           
        User writer = userService.getUserDao().getUserById(article.getAuthorId());
        mav.addObject("writer", writer);
    	if(readerService.getReaderFromWriter(writer.getUsername(), readerEmail) != null){
    		mav.addObject("errormessage","Usted ya esta inscripto a este autor.");
    	}else{
    		readerService.addReader(writer.getUsername(), readerEmail);
    	}
    		
        return mav;
    		
    }

    @RequestMapping(value = "/modifyMail/{readerMail}.{readerMailDomainExt}.{readerMailDomainCountry}.{articleId}", method = RequestMethod.GET)
    public ModelAndView displayModifyReaderMailwithDoubleExtention(@PathVariable String readerMail,
    		@PathVariable String readerMailDomainExt,
    		@PathVariable String readerMailDomainCountry,
    		@PathVariable String articleId) {
    	
    	String email;
    	
    	if(readerMailDomainCountry.isEmpty()){
    		 email = readerMail+"."+readerMailDomainExt;
    		articleId = readerMailDomainCountry;
    	}else{
    		 email = readerMail+"."+readerMailDomainExt+"."+readerMailDomainCountry;
    	}
	
        ModelAndView mav = new ModelAndView("public/modify");
        mav.addObject("mailCuenta", email);
        mav.addObject("articleId", articleId);
        return mav;
    }
    
    @RequestMapping(value = "/modifyMail/{readerMail}.{readerMailDomainExt}.{articleId}", method = RequestMethod.GET)
    public ModelAndView displayModifyReaderMailOneExtention(@PathVariable String readerMail,
    		@PathVariable String readerMailDomainExt,
    		@PathVariable String articleId) {
 
    	String email = readerMail+"."+readerMailDomainExt;
 
        ModelAndView mav = new ModelAndView("public/modify");
        mav.addObject("mailCuenta", email);
        mav.addObject("articleId", articleId);
        return mav;
    }
    
   //@RequestMapping(value = "/modifyMail/{readerMail}{articleId}", method = RequestMethod.GET)
    public ModelAndView displayModifyReaderMail(@PathVariable String readerMail,
    		@PathVariable String articleId) {
     			
        ModelAndView mav = new ModelAndView("public/modify");
        mav.addObject("mailCuenta", readerMail);
        mav.addObject("articleId", articleId);
        return mav;
    }
    
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public ModelAndView modifyReaderMail(@RequestParam ("newEmail") String readerMail,
    		@RequestParam ("readerMail") String oldMail,
    		@RequestParam ("articleId") String articleId) {
    	
    	Article article = articleService.getArticle(articleId);
    	User writer = userService.getUserDao().getUserById(article.getAuthorId());    	
    	
    	ArrayList <Reader> readers =(ArrayList <Reader>)readerService.getReadersList(writer.getUsername());
        for(Reader reader:readers){
        	if(reader.getReaderMail().equalsIgnoreCase(oldMail)){
        		reader.setReaderMail(readerMail);
        		readerService.persist(reader);
        	}
        }

        return displayModifyReaderMail(readerMail,writer.getUsername());
    }
    

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ModelAndView delete(@RequestParam("readerMail") String readerMail,
    								@RequestParam ("articleId") String articleId) {
    	
    	Article article = articleService.getArticle(articleId);
    	User writer = userService.getUserDao().getUserById(article.getAuthorId());    	
    	
        readerService.deleteReaderFromWriter(writer.getUsername(),readerMail);

        ModelAndView mav = new ModelAndView("public/home");
        return mav;
    }
    
    @RequestMapping(value = "/delete/{readerMail}.{readerMailDomainExt}.{readerMailDomainCountry}.{articleId}", method = RequestMethod.GET)
    public ModelAndView deleteReaderMailwithDoubleExtention(@PathVariable String readerMail,
    		@PathVariable String readerMailDomainExt,
    		@PathVariable String readerMailDomainCountry,
    		@PathVariable String articleId) {
    	
    	String email;
    	
    	if(readerMailDomainCountry.isEmpty()){
    		 email = readerMail+"."+readerMailDomainExt;
    		articleId = readerMailDomainCountry;
    	}else{
    		 email = readerMail+"."+readerMailDomainExt+"."+readerMailDomainCountry;
    	}
    	Article article = articleService.getArticle(articleId);
    	User writer = userService.getUserDao().getUserById(article.getAuthorId());    	
    	
        ModelAndView mav = new ModelAndView("public/deleteSubscription");
        mav.addObject("mailCuenta", email);
        mav.addObject("articleId", articleId);
        mav.addObject("writerMail", writer.getUsername());
        return mav;
    }
    
    @RequestMapping(value = "/delete/{readerMail}.{readerMailDomainExt}.{articleId}", method = RequestMethod.GET)
    public ModelAndView deleteReaderMailOneExtention(@PathVariable String readerMail,
    		@PathVariable String readerMailDomainExt,
    		@PathVariable String articleId) {
 
    	String email = readerMail+"."+readerMailDomainExt;
 
    	Article article = articleService.getArticle(articleId);
    	User writer = userService.getUserDao().getUserById(article.getAuthorId());    	
    	
        ModelAndView mav = new ModelAndView("public/deleteSubscription");
        mav.addObject("mailCuenta", email);
        mav.addObject("articleId", articleId);
        mav.addObject("writerMail", writer.getUsername());
        return mav;
    }
}
